import { Util } from "../utils";

/**
 * @description Sheet交联UI服务
 * @export
 * @class AppSheetService
 */
export default class AppSheetService {

    /**
     * @description 主键
     * @private
     * @type {string}
     * @memberof AppSheetService
     */
    private key: string = '';

    /**
     * @description 用户传入参数
     * @private
     * @type {*}
     * @memberof AppSheetService
     */
    private userOptions: any = {};

    /**
     * @description 默认数据
     * @private
     * @type {*}
     * @memberof AppSheetService
     */
    private defaultOpts = {
        container: '', // 设定DOM容器的id
        title: '容器', // 设定名称
        lang: 'zh', // 设定语言
        gridKey: '',
        showinfobar: false,
        showtoolbar: false,
        showtoolbarConfig: {
            undoRedo: true, //撤销重做按钮
            paintFormat: true,  //格式刷
            font: true, // '字体'
            fontSize: true, // '字号大小'
            bold: true, // '粗体 (Ctrl+B)'
            italic: true, // '斜体 (Ctrl+I)'
            strikethrough: true, // '删除线 (Alt+Shift+5)'
            underline: true, // '下划线 (Alt+Shift+6)'
            textColor: true, // '文本颜色'
            fillColor: true, // '单元格颜色'
            border: true, // '边框'
            horizontalAlignMode: true, // '水平对齐方式'
            verticalAlignMode: true, // '垂直对齐方式'
            textWrapMode: true, // '换行方式'
            textRotateMode: true, // '文本旋转方式'
        },
        enableAddRow: false,
        enableAddBackTop: false,
        sheetFormulaBar: false,
        showstatisticBar: false,
        showsheetbar: false,
        rowHeaderWidth: 46,
        columnHeaderHeight: 20,
        data: [
            {
                "name": "工作表", //工作表名称
                "color": "#000000", //工作表颜色
                "index": 0, //工作表索引
                "status": 1, //激活状态
                "order": 0, //工作表的下标
                "hide": 0,//是否隐藏
                "defaultRowHeight": 19, //自定义行高
                "defaultColWidth": 79, //自定义列宽
                "celldata": [], // 单元格数据
                "scrollLeft": 0, //左右滚动条位置
                "scrollTop": 0, //上下滚动条位置
                "luckysheet_select_save": [], //选中的区域
                "calcChain": [],//公式链
                "isPivotTable": false,//是否数据透视表
                "pivotTable": {},//数据透视表设置
                "filter": null,//筛选配置
                "luckysheet_alternateformat_save": [], //交替颜色
                "luckysheet_alternateformat_save_modelCustom": [], //自定义交替颜色	
                "luckysheet_conditionformat_save": {},//条件格式
                "frozen": {}, //冻结行列配置
                "chart": [], //图表配置
                "zoomRatio": 1, // 缩放比例
                "image": [], //图片
                "showGridLines": 0, //是否显示网格线
                "dataVerification": {} //数据验证配置
            }
        ]
    };

    /**
     * @description 控件项数据
     * @private
     * @type {any[]}
     * @memberof AppSheetService
     */
    private ctrlItems: any[] = [];


    /**
     * Creates an instance of AppSheetService.
     * 私有构造，拒绝通过 new 创建对象
     *
     * @memberof AppSheetService
     */
    public constructor(options?: any) {
        this.key = options.key;
        this.userOptions = options.userOptions;
        this.initBasicConf();
    }

    /**
     * @description 初始化基础配置
     * @memberof AppSheetService
     */
    public initBasicConf() {
        Object.assign(this.defaultOpts, this.userOptions);
    }

    /**
     * @description 设置控件options
     * @param {string} id
     * @param {*} opts
     * @memberof AppSheetService
     */
    public setSheetCtrlOpts(id: string, opts: any) {
        if (this.ctrlItems && this.ctrlItems.length > 0) {
            const index = this.ctrlItems.findIndex((ele: any) => {
                return ele.id === id;
            })
            if (index === -1) {
                this.ctrlItems.push(opts);
            } else {
                this.ctrlItems[index] = opts;
            }
        } else {
            this.ctrlItems.push(opts);
        }
        this.saveSheetOpts();
    }

    /**
     * @description 获取控件options
     * @return {*} 
     * @memberof AppSheetService
     */
    public getSheetCtrlOpts() {
        return this.ctrlItems;
    }

    /**
     * @description 获取控件options
     * @return {*} 
     * @memberof AppSheetService
     */
    public getSheetCtrlOptById(id: string) {
        return this.ctrlItems.find((item: any) => {
            return item.id === id;
        });
    }

    /**
     * @description 通过id删除控件options
     * @param {string} id 控件id
     * @memberof AppSheetService
     */
    public removeSheetCtrlOpt(id: string) {
        this.ctrlItems = this.getSheetCtrlOpts().filter((item: any) => {
            return item.id !== id;
        });
        this.saveSheetOpts();
    }

    /**
     * @description 获取sheet参数
     * @memberof AppSheetService
     */
    public async getSheetOpts(args?: any) {
        const result = await this.loadSheetOpts(args);
        if (result.items) {
            this.ctrlItems = result.items;
        }
        if (result.options) {
            this.defaultOpts = result.options;
        }
        return Util.deepCopy(this.defaultOpts);
    }

    /**
     * @description 加载数据
     * @param {*} [args]
     * @memberof AppSheetService
     */
    public async loadSheetOpts(args?: any) {
        if (localStorage.getItem(`${this.key}-ibiz-sheet`)) {
            const resultOpts = JSON.parse(localStorage.getItem(`${this.key}-ibiz-sheet`) as string);
            return resultOpts;
        } else {
            const tempUUID = `sheet-${Util.createUUID()}`;
            if (!this.defaultOpts.container) {
                Object.assign(this.defaultOpts, { container: tempUUID });
            }
            if (!this.defaultOpts.gridKey) {
                Object.assign(this.defaultOpts, { gridKey: tempUUID });
            }
            return Util.deepCopy(this.defaultOpts);
        }
    }

    /**
     * @description 存储sheet参数
     * @memberof AppSheetService
     */
    public async saveSheetOpts() {
        const opts = {
            options: this.defaultOpts,
            items: this.ctrlItems
        };
        localStorage.setItem(`${this.key}-ibiz-sheet`, JSON.stringify(opts));
    }
}